﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
 * ### HTTP://THEAJ.NET/ <AJ@THEAJ.NET> ### *
\* ######################################## */
using System;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using AJRavindiran.Jolt.Utilities;
using AJRavindiran.Jolt.RuneScape.Model;
using AJRavindiran.Jolt.RuneScape.Model.Characters;
using AJRavindiran.Jolt.RuneScape.Network.Protocols;

namespace AJRavindiran.Jolt.RuneScape
{
    public class GameEngine
    {
        #region Fields
        private CharacterManager mCharacterManager = new CharacterManager();



        /// <summary>
        /// GameEnine's core thread. Processes most player-sided things.
        /// </summary>
        private Thread mEngineThread;

        /// <summary>
        /// Determines if the engine is running or not.
        /// </summary>
        private static bool mEngineRunning = true;
        #endregion

        #region Constructors
        public GameEngine()
        {
            // Start the game engine on the higest possible 
            // thread priority for smooth game play.
            mEngineThread = new Thread(new ThreadStart(run));
            mEngineThread.Priority = ThreadPriority.Highest;
            mEngineThread.Start();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets a bool if the engine is running or not.
        /// </summary>
        public bool EngineRunning
        {
            get { return mEngineRunning; }
            set { mEngineRunning = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// RuneScape Environment's main process.
        /// <para>This processes at a rate of 100 milliseconds, and the processing time
        /// is subtracted from the amount of time it sleeps to keep at a rate of 100
        /// milliseonds. Packets are checked every 100 milliseconds for faster response 
        /// time, and most other updates are processed every 600 milliseconds. Most
        /// character interactions are ran from this thread.</para>
        /// </summary>
        private void run()
        {
            double lastUpdate = 0;
            double currentTime;

            try
            {
                while (mEngineRunning)
                {
                    currentTime = TimeUtilities.GetCurrentMilliseconds();
                    if (currentTime - lastUpdate >= 600)
                    {
                        mCharacterManager.ConnectPlayers();
                        //JoltEnvironment.GetLogger().WriteClean(Convert.ToString(DateTime.Now.ToString("hh:mm:ss.FFF") + " - " + DateTime.Now.ToLongDateString()));

                        // Set the last update time to currentTime, so we can have a smoothly running thread.
                        lastUpdate = currentTime;
                    }
                    try
                    {
                        Thread.Sleep(100 - (int)(TimeUtilities.GetCurrentMilliseconds() - currentTime));
                    }
                    catch (Exception e) { JoltEnvironment.GetLogger().WriteException(e); }
                }
            }
            catch (Exception e) { JoltEnvironment.GetLogger().WriteException(e); }
        }

        /// <summary>
        /// Accessor for the engine's thread.
        /// </summary>
        public Thread GetThread()
        {
            return mEngineThread;
        }

        public CharacterManager GetCharacterManager()
        {
            return mCharacterManager;
        }
        #endregion
    }
}
